﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class UomBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IUomDA uomDA = DataAccess.CreateUomDA();

        public UomBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public UomBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> uomIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<UomInfo> uomInfos = new List<UomInfo>();
                UomInfo uomInfo = null;
                for (i = 0; i < uomIDS.Count; i++)
                {
                    uomInfo = uomDA.GetUomByID(dataBase, tran, uomIDS[i], this.dataBaseID[0], this.pubParasInfo);
                    uomInfos.Add(uomInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (uomDA.DeleteUom(dataBase, tran, uomIDS, this.dataBaseID) > 0)
                {
                    for (i = 0; i < uomIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<UomInfo>(dataBase, tran, uomInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "UOM", this.dataBaseID[0]);
                        temp = temp + uomIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return uomDA.GetAllUom(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string uomID)
        {
            return uomDA.GetUomByID(uomID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            UomQueryEntity uomQuery = obj as UomQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(uomQuery.IsActive))
            {
                sqlWhere.Add(" UOM.IS_ACTIVE " + uomQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(uomQuery.UomID))
            {
                sqlWhere.Add(" UOM.UOM_ID " + uomQuery.UomID);
            }
            if (!string.IsNullOrEmpty(uomQuery.UomDescr))
            {
                sqlWhere.Add(" UOM.UOM_DESCR " + uomQuery.UomDescr);
            }
            return uomDA.GetUomByQueryList(sqlWhere, uomQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string uomID = string.Empty;
            try
            {
                UomInfo uom = obj as UomInfo;
                if (uomDA.CheckUomIDUnique(uom.UomID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2250_001", this.pubParasInfo.UserLanguage, new List<string> { uom.UomID });
                }
                uom.CreatedDate = DateTime.Now;
                uom.UpdatedDate = DateTime.Now;
                uom.UpdatedBy = uom.CreatedBy;
                if (uomDA.InsertUom(uom, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UomInfo>(null, uom, AuditAction.Insert, this.pubParasInfo.UserID, "UOM", this.dataBaseID[0]);
                    uomID = uom.UomID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return uomID;
        }

        public string Update(object obj)
        {
            string uomID = string.Empty;
            try
            {
                UomInfo uom = obj as UomInfo;
                uom.UpdatedDate = DateTime.Now;
                UomInfo oldUomInfo = uomDA.GetUomByID(uom.UomID, this.dataBaseID[0], this.pubParasInfo);
                if (uomDA.UpdateUom(uom, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UomInfo>(oldUomInfo, uom, AuditAction.Update, this.pubParasInfo.UserID, "UOM", this.dataBaseID[0]);
                    uomID = uom.UomID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return uomID;
        }
    }
}

